package com.work.leecode.array.middle;

/**
 * @author huangxianjin <huangxianjin@kuaishou.com>
 * Created on 2024-04-11
 */

import java.util.Arrays;

/**
 * 轮转数组---189
 */
public class Lc189 {
    public static void main(String[] args) {
        int[] nums = {1, 6, 3, 5, 6};
        int k = 3;
        rotate(nums, k);
        System.out.println(Arrays.toString(nums));
    }

    //方法一：使用额外的数组
    public static void rotate(int[] nums, int k) {
        int len = nums.length;

        //存储最终结果的数组
        int[] res = new int[len];
        for (int i = 0; i < len; i++) {
            res[(i + k) % len] = nums[i];
        }
        //将最终结果的数组复制到原数组中
        System.arraycopy(res, 0, nums, 0, len);
    }
}
